package com.blog.utils;

import java.util.Map;
import java.util.HashMap;

/**
 * 返回值封闭 常用于ajax
 */
@SuppressWarnings({"serial", "rawtypes", "unchecked"})
public class JsonResult extends HashMap{
    private static final String STATUS = "status";

    public JsonResult() {
    }

    public static JsonResult ok() {
        return new JsonResult().setOk();
    }

    public static JsonResult ok(String msg) {
        return new JsonResult().setOk().set("msg", msg);
    }

    public static JsonResult ok(Object key, Object value) {
        return ok().set(key, value);
    }

    public static JsonResult fail() {
        return new JsonResult().setFail();
    }

    public static JsonResult fail(String msg) {
        return new JsonResult().setFail().set("msg", msg);
    }

    public static JsonResult fail(Object key, Object value) {
        return fail().set(key, value);
    }
    public static JsonResult create() {
        return new JsonResult();
    }

    public static JsonResult create(Object key, Object value) {
        return new JsonResult().set(key, value);
    }
    public JsonResult setOk(){
        super.put(STATUS,200);
        return this;
    }

    public JsonResult setFail(){
        super.put(STATUS,201);
        return this;
    }


    public JsonResult set(Object key, Object value) {
        super.put(key, value);
        return this;
    }

    public JsonResult set(Map map) {
        super.putAll(map);
        return this;
    }

    public JsonResult set(JsonResult ret) {
        super.putAll(ret);
        return this;
    }

    public Integer getInt(Object key){
        return (Integer) get(key);
    }

    public boolean isOk(){
        Integer code = (Integer) getInt(STATUS);
        if(code != null && code == 200){
            return true;
        }else{
            return false;
        }
    }

    public boolean isFail(){
        Integer code = (Integer) getInt(STATUS);
        if(code !=200){
            return true;
        }else{
            return false;
        }
    }

}
